package com.teamsun.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    //十六进制下数字到字符的映射数组 
    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
 
    
    public static String md5UpperCase(String originString){
    	return encodeByMD5(originString,"UTF-8");
    }
	public static String md5LowerCase(String originString){
		return encodeByMD5(originString,"UTF-8").toLowerCase();
	}


	/**对字符串进行MD5编码*/
    private static String encodeByMD5(String originString, String encodeStr){
        if (originString != null) {
            try {
                //创建具有指定算法名称的信息摘要 
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                //使用指定的字节数组对摘要进行最后更新，然后完成摘要计算 
                byte[] results = md5.digest(originString.getBytes(encodeStr));
                //将得到的字节数组变成字符串返回  
                String result = byteArrayToHexString(results);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
 
    /** 
    * 轮换字节数组为十六进制字符串 
    * @param b 字节数组 
    * @return 十六进制字符串 
    */ 
    private static String byteArrayToHexString(byte[] b){
        StringBuffer resultSb = new StringBuffer();
        for(int i = 0; i < b.length; i++){ 
            resultSb.append(byteToHexString(b[i])); 
        } 
        return resultSb.toString(); 
    }
 
    //将一个字节转化成十六进制形式的字符串 
    private static String byteToHexString(byte b){
        int n = b;
        if(n < 0){
        	n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }
    
    
    /**
	 * md5 16位加密
	 * 
	 * @param str
	 * @return 16位加密字符
	 */
	public static String getMD516(String str )
	{
		MessageDigest messageDigest = null;
		try
		{
			messageDigest = MessageDigest.getInstance( "MD5" );

			messageDigest.reset();

			messageDigest.update( str.getBytes( "UTF-8" ) );
		}
		catch( NoSuchAlgorithmException e )
		{
			System.out.println( "NoSuchAlgorithmException caught!" );
			System.exit( -1 );
		}
		catch( UnsupportedEncodingException e )
		{
			e.printStackTrace();
		}
		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for( int i = 0; i < byteArray.length; i++ )
		{
			if( Integer.toHexString( 0xFF & byteArray[i] ).length() == 1 ) {
				md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
			}
			else{
				md5StrBuff.append( Integer.toHexString( 0xFF & byteArray[i] ) );
			}
		}
		// 16位加密，从第9位到25位
		return md5StrBuff.substring( 8, 24 ).toString().toUpperCase();
	}

	/**
	 * md5 32位加密
	 * 
	 * @param sourceStr
	 * @return 32位加密字符
	 */
	public static String getMd532(String sourceStr )
	{
		String result = "";
		try
		{
			MessageDigest md = MessageDigest.getInstance( "MD5" );
			md.update( sourceStr.getBytes() );
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer( "" );
			for( int offset = 0; offset < b.length; offset++ )
			{
				i = b[offset];
				if( i < 0 )
					i += 256;
				if( i < 16 )
					buf.append( "0" );
				buf.append( Integer.toHexString( i ) );
			}
			result = buf.toString();
		}
		catch( NoSuchAlgorithmException e )
		{
			System.out.println( "NoSuchAlgorithmException caught!" );
		}
		return result.toUpperCase();
	}

	public static String getMd532l(String sourceStr )
	{
		String result = "";
		try
		{
			MessageDigest md = MessageDigest.getInstance( "MD5" );
			md.update( sourceStr.getBytes() );
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer( "" );
			for( int offset = 0; offset < b.length; offset++ )
			{
				i = b[offset];
				if( i < 0 )
					i += 256;
				if( i < 16 )
					buf.append( "0" );
				buf.append( Integer.toHexString( i ) );
			}
			result = buf.toString();
		}
		catch( NoSuchAlgorithmException e )
		{
			System.out.println( "NoSuchAlgorithmException caught!" );
		}
		return result.toLowerCase();
	}

    
    public static void main(String[] args) {
		System.out.println(md5LowerCase("rptADMIN"));
	}
}